package com.supply.dl.activity.seachmode;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.util.ArrayMap;
import android.view.View;

import com.supply.dl.R;
import com.supply.dl.activity.BaseActivity;
import com.supply.dl.activity.NoticeInAndOutboundDetailsActivity;
import com.supply.dl.app.AppConfig;
import com.supply.dl.app.adapter.NormalAdapter;
import com.supply.dl.app.adapter.ViewHolder;
import com.supply.dl.app.http.BaseInfo;
import com.supply.dl.bean.BaseItemBean;
import com.supply.dl.bean.InAndOutBoundLocationBean;
import com.supply.dl.bean.request.NoticeRequest;
import com.supply.dl.constant.Constants;
import com.supply.dl.http.HttpManger;
import com.supply.dl.utils.DateUtils;
import com.supply.dl.utils.StringUtils;
import com.supply.dl.utils.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ${qianzhihe} on 2018/8/5.
 * 通知单入库列表
 * 通知单出库列表
 */

public class NoticeLetterInAndOutBoundModle extends BaseModle{

    @Override
    public void setSearchAdapter() {
        super.setSearchAdapter();
        if (adapter != null)
            return;
        resource = new ArrayList<>();
        adapter=new NormalAdapter<BaseItemBean>(context, resource) {
            @Override
            protected void convert(ViewHolder holder, BaseItemBean item) {
                if (isInBound) {
                    holder.setText(R.id.inbound_location_main_item_inboundNoticeLetterNo, getResourcesString(R.string.inboundNoticeLetterNo));
                } else {
                    holder.setText(R.id.inbound_location_main_item_inboundNoticeLetterNo, getResourcesString(R.string.outboundNoticeLetterNo));
                }
                InAndOutBoundLocationBean noticeInOrOutBoundBean = (InAndOutBoundLocationBean) item.object;
                holder.setText(R.id.inbound_location_main_item_purchaseContentTv, noticeInOrOutBoundBean.bussTypeName);//业务类型
                holder.setText(R.id.inbound_location_main_item_positionStatusName, noticeInOrOutBoundBean.displayNodOrderStatusName);//入库状态（根据设计要求而修改）
                holder.setText(R.id.inbound_location_main_item_purchasePersonNameTv, noticeInOrOutBoundBean.driverName);//驾驶员姓名
                holder.setText(R.id.inbound_location_main_item_carIdCardTv, noticeInOrOutBoundBean.carNo);//车牌号
                holder.setText(R.id.inbound_location_main_item_timeTv, StringUtils.isEmpty(String.valueOf(noticeInOrOutBoundBean.addTime)) ? "" : DateUtils.getSimpleDateTimeByMill(String.valueOf(noticeInOrOutBoundBean.addTime), DateUtils.DATE_TIME_FORMAT));//建单时间
                holder.setText(R.id.inbound_location_main_item_showInboundNoticeLetterNo, noticeInOrOutBoundBean.noticeOrderNo);//入库通知单号
                holder.setText(R.id.inbound_location_main_item_showERPNo, noticeInOrOutBoundBean.erpOrderNo);//ERP订单号
                holder.getView(R.id.inbound_location_main_item_purchaseTypeTv).setVisibility(View.INVISIBLE);
                holder.getView(R.id.inbound_location_main_item_lyParent).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    }
                });
            }

            @Override
            protected int getItemLayoutId() {
                return R.layout.inbound_location_main_item;
            }
        };

        adapter.setOnItemClickListener(this);
    }

    @Override
    public void setHintText() {
        super.setHintText();
        hintText = getResourcesString(R.string.hintSearchConditionForInBoundLocation);
    }

    @Override
    public void setTitleShow() {
        super.setTitleShow();
        if (mActivityFlag== Constants.FLAG_ACTIVITY_NOTICE_LETTER_IN_BOUND_UI){
            isInBound=true;
            titleShow=getResourcesString(R.string.notice_to_inbound);
        }else if (mActivityFlag==Constants.FLAG_ACTIVITY_NOTICE_LETTER_OUT_BOUND_UI){
            isInBound=false;
            titleShow=getResourcesString(R.string.notice_to_out_outbound);
        }
    }

    @Override
    public void init(Context context) {
        super.init(context);
        this.context = context;
        setTitleShow();
        setSearchAdapter();
    }

    @Override
    public void vistData(String keyWord, ArrayMap<String, String> filterMap, OnVistDataSuccess onVistDataSuccess) {
        super.vistData(keyWord, filterMap, onVistDataSuccess);
        getNoticeInOrOutBoundList(keyWord, filterMap);
        if (onVistDataSuccess != null){
            onVistDataSuccess.showUi(resource.size() == 0);
        }
    }

    @Override
    public void onItemClick(ViewHolder viewHolder, BaseItemBean itemBean, int position) {
        super.onItemClick(viewHolder, itemBean, position);
        Bundle bundle=new Bundle();
        if (isInBound){
            bundle.putInt(Constants.FLAG_ACTIVITY_KEY, Constants.FLAG_ACTIVITY_NOTICE_IN_BOUND_DETAILS_UI);
        }else {
            bundle.putInt(Constants.FLAG_ACTIVITY_KEY, Constants.FLAG_ACTIVITY_NOTICE_OUT_BOUND_DETAILS_UI);
        }
        bundle.putSerializable("InAndOutBoundLocationBean", (InAndOutBoundLocationBean)itemBean.object);
        ((BaseActivity) context).jumpActivity(NoticeInAndOutboundDetailsActivity.class, bundle);
    }

    @Override
    public void onSuccess(BaseInfo<?> baseInfo) {
        super.onSuccess(baseInfo);
        List<InAndOutBoundLocationBean> dataList= (List<InAndOutBoundLocationBean>) baseInfo.getData();
        if(pageNum == 1){
            resource.clear();
        }
        if(dataList.size() < 10){
            hasMoreData = false;
        }
        if (dataList != null && dataList.size()>0) {
            for (InAndOutBoundLocationBean inAndOutBoundLocationBean : dataList){
                resource.add(new BaseItemBean(inAndOutBoundLocationBean));
            }
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onFailure(String message) {
        super.onFailure(message);
        ToastUtil.showToast(context, message);
    }

    /**
     * 获取通知单入库、出库列表数据
     */
    private void getNoticeInOrOutBoundList(String keyWord, ArrayMap<String, String> filterMap){
        String inBoundStatus="";//入库状态
        String bussType="";//业务类型
        String companyId="";//公司
        String factoryId="";//工厂
        String productWorkshopId="";//车间
        String createTimeStart="";//建单开始时间
        String createTimeEnd="";//建单结束时间
        if (filterMap.size()>0){
            //取用户选择的入库状态
            inBoundStatus = filterMap.get(AppConfig.SearchFilterAction.IN_HOUSER_STATUS)==null ? "": filterMap.get(AppConfig.SearchFilterAction.IN_HOUSER_STATUS);
            bussType = filterMap.get(AppConfig.SearchFilterAction.BUSINESS_TYPE)==null ? "": filterMap.get(AppConfig.SearchFilterAction.BUSINESS_TYPE);
            companyId = filterMap.get(AppConfig.SearchFilterAction.COMPANY)==null ? "": filterMap.get(AppConfig.SearchFilterAction.COMPANY);
            factoryId = filterMap.get(AppConfig.SearchFilterAction.FACTORY)==null ? "": filterMap.get(AppConfig.SearchFilterAction.FACTORY);
            productWorkshopId = filterMap.get(AppConfig.SearchFilterAction.WORKSHOP)==null ? "": filterMap.get(AppConfig.SearchFilterAction.WORKSHOP);
            createTimeStart = filterMap.get(AppConfig.SearchFilterAction.START_TIME)==null ? "": filterMap.get(AppConfig.SearchFilterAction.START_TIME);
            createTimeEnd = filterMap.get(AppConfig.SearchFilterAction.END_TIME)==null ? "": filterMap.get(AppConfig.SearchFilterAction.END_TIME);
        }
        HttpManger httpManger= HttpManger.getInstance();
        httpManger.setContext(context);
        NoticeRequest noticeRequest=new NoticeRequest();
        noticeRequest.key=keyWord;
        noticeRequest.displayNodOrderStatus=inBoundStatus;
        noticeRequest.bussType=bussType;
        noticeRequest.companyId=companyId;
        noticeRequest.factoryId=factoryId;
        noticeRequest.productWorkshopId=productWorkshopId;
        noticeRequest.createTimeStart=createTimeStart;
        noticeRequest.createTimeEnd=createTimeEnd;
        if (isInBound){
            httpManger.getNoticeInBoundList(noticeRequest, String.valueOf(pageNum));
        }else {
            httpManger.getNoticeOutBoundList(noticeRequest, String.valueOf(pageNum));
        }
        httpManger.setHttpResponseCallBackListener(this);
    }

}
